package com.neusoft.ecommerce.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.ActivityType;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.service.ActivityTypeService;
import com.neusoft.ecommerce.service.GoodsService;

/**
 * 
 * @author YinKuenTam
 *
 */
@RestController
public class ActivityTypeController {
	private final ActivityTypeService activityTypeService;
	private final GoodsService goodsService;
	
	public ActivityTypeController(ActivityTypeService activityTypeService,GoodsService goodsService) {
		this.activityTypeService = activityTypeService;
		this.goodsService = goodsService;
		
	}
	@GetMapping("/activityType_page")
    public ModelAndView afterSaleServicePage() {
         return new ModelAndView("activityType/activityType-list");
    }
	
    @RequestMapping("/activityType/list")
    @ResponseBody
    public LayuiPager<ActivityType> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<ActivityType> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = activityTypeService.findTotal(pager);
        List<ActivityType> list = activityTypeService.findList(pager);
        for (ActivityType activityType : list) {
            System.out.println(activityType);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }
    @GetMapping("/addActivityType")
    public ModelAndView addActivityType() {
        return new ModelAndView("activityType/activityType-add");
    }
    @RequestMapping("/activityType/goodsList")
    public List<Goods> findIndentList() {
	    return goodsService.findAll();
    }
    
    
    @PostMapping("/activityType/add")
    public Map<String, Object> add(ActivityType activityType) {
        System.out.println(activityTypeService);
        Map<String, Object> map = new HashMap<>(16);
        if (activityTypeService.add(activityType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
    @GetMapping("/activityType/edit_page")
    public ModelAndView editPage(ActivityType activityType, Model model) {
        System.out.println(activityType);
        LayuiPager<ActivityType> pager = new LayuiPager<>(1,1);
        pager.setCondition(activityType);
        ActivityType message = activityTypeService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("activityType/activityType-edit");
        model.addAttribute("activityType", message);
        return view;
    }
    @PostMapping("/activityType/edit")
    public Map<String, Object> edit(ActivityType activityType) {
        System.out.println(activityType);
        Map<String, Object> map = new HashMap<>(16);
        if (activityTypeService.edit(activityType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	@PostMapping("/activityType/delete")
    public Map<String, Object> delete(ActivityType activityType) {
        System.out.println(activityType);
        Map<String, Object> map = new HashMap<>(16);
        if (activityTypeService.delete(activityType)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }
	


}
